/**
 * @file highLight.js
 * @desc {{description}}{{代码高亮}}
 * @doc: https://ithelp.ithome.com.tw/articles/10204044
 * @createTime 2019年03月07日19:30:50
 */
import hljs from './hljs.js'

const maybe = f => {
  try {
    return f()
  }
  catch (e) {
    return false
  }
}

// Highlight with given language.
const highlight = (code, lang) =>
  maybe(() => hljs.highlight(lang, code, true).value) || ''

// Highlight with given language or automatically.
const highlightAuto = (code, lang) =>
  lang
    ? highlight(code, lang)
    : maybe(() => hljs.highlightAuto(code).value) || ''

// Wrap a render function to add `hljs` class to code blocks.
const wrap = render => (...args) => {
  return render.apply(this, args)
    .replace('<code class="', '<code class="hljs ')
    .replace('<code>', '<code class="hljs">')
}

const highlightjs = (md, opts) => {
  opts = Object.assign({}, highlightjs.defaults, opts)

  md.options.highlight = opts.auto ? highlightAuto : highlight
  md.renderer.rules.fence = wrap(md.renderer.rules.fence)

  if (opts.code) {
    md.renderer.rules.code_block = wrap(md.renderer.rules.code_block)
  }
}

highlightjs.defaults = {
  auto: true,
  code: true
}

export default highlightjs
